﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Cleware.Debit3.Tools.EdmUtil
{
    public class Parameters
    {
        private string _edmx;
        private string _ssdl;
        private string _csdl;
        private string _msl;
        private string _connectionString;
        private List<string> _spList;
        private List<string> _exclusions;

        private string xmldescriptions;

        private const string HIBASPARAMETER = "Hibás paraméter: {0} - {1}\n";

        
        public string Edmx
        {
            get 
            { 
                if (string.IsNullOrEmpty(_edmx))
                    _edmx = Config.Instance.Edmx;
                return _edmx;
            }
            set { _edmx = value; }
        }
        public string Ssdl
        {
            get
            {
                if (string.IsNullOrEmpty(_ssdl))
                    _ssdl = Config.Instance.Ssdl;
                return _ssdl;
            }
            set { _ssdl = value; }
        }
        public string Csdl
        {
            get
            {
                if (string.IsNullOrEmpty(_csdl))
                    _csdl = Config.Instance.Csdl;
                return _csdl;
            }
            set { _csdl = value; }
        }
        public string Msl
        {
            get
            {
                if (string.IsNullOrEmpty(_msl))
                    _msl = Config.Instance.Msl;
                return _msl;
            }
            set { _msl = value; }
        }
        public string ConnectionString
        {
            get
            {
                if (string.IsNullOrEmpty(_connectionString))
                    _connectionString = Config.Instance.ConnectionString;
                return _connectionString;
            }
            set { _connectionString = value; }
        }
        public List<string> SPList
        {
            get
            {
                if (_spList == null)
                    _spList = Config.Instance.SPList;
                return _spList;
            }
            set { _spList = value; }
        }
        public List<string> Exclusions
        {
            get
            {
                if (_exclusions == null)
                    _exclusions  = Config.Instance.Exclusions;
                return _exclusions;
            }
            set { _exclusions = value; }
        }

        public string XmlDescription
        {
            get
            {
                if (xmldescriptions == null)
                    xmldescriptions = Config.Instance.XmlDescription;
                return xmldescriptions;
            }
            set { xmldescriptions = value; }
        }

        public Parameters()
        {
            Edmx = Ssdl = Csdl = Msl = ConnectionString = null;
            SPList = Exclusions = null;
            xmldescriptions = null;
        }
        

        public bool IsValid (out string message)
        {
            bool isValid = true;
            message = string.Empty;

            if (string.IsNullOrEmpty(this.Edmx) || (!File.Exists(this.Edmx)))
            {
                message += string.Format(HIBASPARAMETER, "edmx", this.Edmx);
                isValid = false;
            }

            if (string.IsNullOrEmpty(this.Ssdl) || (!File.Exists(this.Ssdl)))
            {
                message += string.Format(HIBASPARAMETER, "ssdl", this.Ssdl);
                isValid = false;
            }

            if (string.IsNullOrEmpty(this.Csdl) || (!File.Exists(this.Csdl)))
            {
                message += string.Format(HIBASPARAMETER, "csdl", this.Csdl);
                isValid = false;
            }

            if (string.IsNullOrEmpty(this.Msl) || (!File.Exists(this.Msl)))
            {
                message += string.Format(HIBASPARAMETER, "msl", this.Msl);
                isValid = false;
            }

            if (string.IsNullOrEmpty(this.ConnectionString) || (!DA.ConnectionIsValid(this.ConnectionString)))
            {
                message += string.Format(HIBASPARAMETER, "db", this.ConnectionString);
                isValid = false;
            }

            return isValid;
        }
    }
}
